package com.agu.mgt.spmgt.controller;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import com.agu.common.annotation.SysLog;
import com.agu.common.excel.Column;
import com.agu.common.excel.ExcelExport;

import com.agu.utils.xss.XssHttpServletRequestWrapper;
import com.alibaba.fastjson.JSON;
import com.agu.mgt.sys.controller.AbstractController;
import com.agu.entity.spmgt.EntSpUserEntity;
import com.agu.mgt.spmgt.service.EntSpUserService;
import com.agu.utils.Query;
import com.github.pagehelper.Page;
import com.agu.result.Result;
import com.agu.result.ResultGenerator;
import com.agu.utils.CompareUtils;
import com.agu.utils.FormatUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * SP用户表
 * 
 * @author 
 * @email 
 * @date 2019-01-16 11:09:40
 */
 @SuppressWarnings("rawtypes")
@RestController
@Api("SP用户表")
@RequestMapping("entspuser")
public class EntSpUserController extends AbstractController{
	@Autowired
	private EntSpUserService entSpUserService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@ApiOperation(value = "列表", notes = "说明")
	@RequiresPermissions("entspuser:list")
	public Result list(@RequestBody Map params){

		// 查询列表数据
		Query query = new Query(params);
		Page<EntSpUserEntity> page = entSpUserService.queryPageList(query);
		return ResultGenerator.successPage(page);
	}
	
	
	/**
	 * 信息
	 */
	@GetMapping("/info/{id}")
	@ApiOperation(value = "信息", notes = "说明")
	@RequiresPermissions("entspuser:info")
	public Result info(@PathVariable("id") Long id){
		EntSpUserEntity entSpUser = entSpUserService.queryObject(id);
		
		return ResultGenerator.successData(entSpUser);
	}
	
	/**
	 * 保存
	 */
	@SysLog("新增SP用户表")
	@PostMapping("/save")
	@ApiOperation(value = "保存", notes = "说明")
	@RequiresPermissions("entspuser:save")
	public Result save(@RequestBody EntSpUserEntity entSpUser){
	
		entSpUserService.save(entSpUser);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		     cmap.put("id","");
		     cmap.put("spId","SPID");
		     cmap.put("userId","用户ID");
		String remark = CompareUtils.getObjectRemark(entSpUser, cmap);
		return ResultGenerator.success("保存成功").setRemark(remark);
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改SP用户表")
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "说明")
	@RequiresPermissions("entspuser:update")
	public Result update(@RequestBody EntSpUserEntity entSpUser){
	
		EntSpUserEntity entSpUser1 = entSpUserService.queryObject(entSpUser.getId());
	
		entSpUserService.update(entSpUser);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		
		     cmap.put("id","");
		     cmap.put("spId","SPID");
		     cmap.put("userId","用户ID");
	
		String remark = CompareUtils.getTwoObjectRemark(entSpUser1, entSpUser, cmap);
	
		return ResultGenerator.success("修改成功").setRemark(remark);
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除SP用户表")
	@GetMapping("/delete/{id}")
	@ApiOperation(value = "删除", notes = "说明")
	@RequiresPermissions("entspuser:delete")
	public Result delete(@PathVariable("id") Long id){
	
	    	if (id == null) {
			return ResultGenerator.fail("缺少参数");
		}
	
		EntSpUserEntity entSpUser = entSpUserService.queryObject(id);
	
		entSpUserService.delete(id);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		     cmap.put("id","");
		     cmap.put("spId","SPID");
		     cmap.put("userId","用户ID");
		String remark = CompareUtils.getObjectRemark(entSpUser, cmap);
		return ResultGenerator.success("删除成功").setRemark(remark);
	}
	
	/**
	 * 批量删除
	 */
	@SysLog("批量删除SP用户表")
	@PostMapping("/deletes")
	@ApiOperation(value = "删除", notes = "说明")
	@RequiresPermissions("entspuser:delete")
	public Result deletes(@RequestBody Long[] ids){
	
		entSpUserService.deleteBatch(ids);
		
		return ResultGenerator.success("删除成功");
	}
	
	@RequestMapping("/download")
	@ApiOperation(value = "下载", notes = "说明")
	@RequiresPermissions("entspuser:list")
	public void download(HttpServletRequest request, HttpServletResponse response) {

		HttpServletRequest orgRequest = XssHttpServletRequestWrapper.getOrgRequest(request);
		
		String query = orgRequest.getParameter("query");

		HashMap querymap = JSON.parseObject(query, HashMap.class);

		List<EntSpUserEntity> list = entSpUserService.queryList(querymap);

		if (list != null && list.size() > 0) {
			Map map = new HashMap();


			List<Column> clist = new ArrayList();
			Column cloumn = null;

            cloumn = new Column("", "id", 1);
			clist.add(cloumn);
            cloumn = new Column("SPID", "spId", 1);
			clist.add(cloumn);
            cloumn = new Column("用户ID", "userId", 1);
			clist.add(cloumn);
			try {
				ExcelExport export = exportList(list, clist, map);
				if (export != null) {
					this.downExcel(export, response);
					//this.downzip(export, response);
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
